﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using GMS.IServices;
using GMS.Models.Dto;
using GMS.Models.Entity;
using GMS.Models.ResponseDto;
using GMS.Models.SearchDto;
using Microsoft.AspNetCore.Mvc;

namespace GMS.API.Controllers
{
    [ApiController]
    [Route("roleInfo")]
    public class RoleInfoController : ControllerBase
    {
        private readonly IMapper _mapper;
        private readonly IRoleInfoService _roleInfoService;

        public RoleInfoController(IMapper mapper, IRoleInfoService roleInfoService)
        {
            _mapper = mapper;
            _roleInfoService = roleInfoService;
        }

        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <returns></returns>
        [HttpGet("All")]
        public async Task<IActionResult> GetAll([FromQuery] RoleInfoSearchDto dto)
        {
            var entities = await _roleInfoService.GetMultipleAsync(dto);
            var responseDtos = _mapper.Map<List<RoleInfoResponseDto>>(entities);
            return StatusCode(200, new { code = 200, message = "获取角色成功", data = responseDtos });
        }

        [HttpGet("Single")]
        public async Task<IActionResult> GetAll([FromQuery] Guid id)
        {
            var entity = await _roleInfoService.GetSingleAsync(id);
            var responseDto = _mapper.Map<RoleInfoResponseDto>(entity);
            return StatusCode(200, new { code = 200, message = "获取角色成功", data = responseDto });
        }

        [HttpPost("Added")]
        public async Task<IActionResult> Added(RoleInfoDto dto)
        {
            var entity = _mapper.Map<RoleInfo>(dto);
            var res = await _roleInfoService.AddedAsync(entity);
            return res > 0
                ? StatusCode(200, new { code = 200, message = "添加角色成功" })
                : BadRequest(new { code = 500, message = "添加角色失败" });
        }

        [HttpPut("Edited")]
        public async Task<IActionResult> Updated(RoleInfoDto dto)
        {
            var entity = _mapper.Map<RoleInfo>(dto);
            var res = await _roleInfoService.UpdatedAsync(entity);
            return res > 0
                ? StatusCode(200, new { code = 200, message = "修改角色成功" })
                : BadRequest(new { code = 500, message = "修改角色失败" });
        }

        [HttpDelete("Deleted")]
        public async Task<IActionResult> Deleted([FromQuery] Guid id)
        {
            var res = await _roleInfoService.DeletedAsync(id);
            return res > 0
                ? StatusCode(200, new { code = 200, message = "删除角色成功" })
                : BadRequest(new { code = 500, message = "删除角色失败" });
        }

    }
}